package com.mmall.controller.backend;


import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategroyService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@ControllerAdvice
@RequestMapping(value = "/manage/categroy/")
public class CategoryManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategroyService iCategroyService;


    /**
     * 添加管理员下面的商品类别
     * @param session
     * @param categroyName
     * @param categroyId
     * @return
     */
    @RequestMapping(value = "addCategroy.do")
    @ResponseBody

    //给形参赋值:@RequestParam(value = "categroyId" ,defaultValue = "0")指定id,如果前端没有传id就直接赋予0这个值,用来设置他说根节点
    public ServerResponse addCategroy(HttpSession session, String categroyName, @RequestParam(value = "categroyId", defaultValue = "0") Integer categroyId) {

        //1.获得session中的数据,并且获得用户名
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {

            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }

        //2.判断用户是否为管理员
        ServerResponse adminCount = iUserService.selectAndadmin(user);

        if (adminCount.isSuccess()) {

            //3.是管理员在做业务处理
            return iCategroyService.addCategroy(categroyName, categroyId);

        } else {
            return ServerResponse.createByErrorMessage("不是管理员,不能添加商品");
        }

        //return ServerResponse.createBySuccessMessage("添加商品失败");
    }


    /**
     * 更新商品类别的信息
     * @param session
     * @param NewcategroyName
     * @param categroyId
     * @return
     */
    @RequestMapping(value = "updateCategroy.do")
    @ResponseBody
    //根据id号更新的是商品的名称,
    public ServerResponse updateCategroy(HttpSession session, String NewcategroyName, Integer categroyId) {
        //1.获得session中的数据,并且获得用户名
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {

            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }

        //2.判断用户是否为管理员
        ServerResponse adminCount = iUserService.selectAndadmin(user);

        if (adminCount.isSuccess()) {

            //3.是管理员在做业务处理,更新商品名称和id
            return iCategroyService.updateCategroy(NewcategroyName, categroyId);

        } else {
            return ServerResponse.createByErrorMessage("不是管理员,不能添加商品");
        }
    }


    /**
     * 查看商品下面的子商品
     * @param session
     * @param parentId
     * @return
     */
    @RequestMapping(value = "selectParentIdAndCategroy.do")
    @ResponseBody
    //@RequestParam(value = "parentId",defaultValue = "0"),用来指定parentId,并且前端没有传数据的时候直接指向根节点
    public ServerResponse selectParentIdAndCategroy(HttpSession session, @RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {
        //1.获得session中的数据,并且获得用户名
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {

            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }

        //2.判断用户是否为管理员
        ServerResponse adminCount = iUserService.selectAndadmin(user);

        if (adminCount.isSuccess()) {

            //3.是管理员在做业务处理,更新商品名称和id
            return iCategroyService.selectParentIdAndCategroy(parentId);

        } else {
            return ServerResponse.createByErrorMessage("不是管理员,不能查看子类商品");
        }
    }


    /**
     * 递归查看商品下面的所有子商品
     * @param session
     * @param parentId
     * @return
     */
    @RequestMapping(value = "selectRecursionAndParentIdAndCategroy.do")
    @ResponseBody
    //@RequestParam(value = "parentId",defaultValue = "0"),用来指定parentId,并且前端没有传数据的时候直接指向根节点
    public ServerResponse selectRecursionAndParentIdAndCategroy(HttpSession session, @RequestParam(value = "parentId", defaultValue = "0") int parentId) {
        //1.获得session中的数据,并且获得用户名
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {

            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }

        //2.判断用户是否为管理员
        ServerResponse adminCount = iUserService.selectAndadmin(user);

        if (adminCount.isSuccess()) {

            //3.是管理员在做业务处理,递归查询根节点下的所有子节点
            return iCategroyService.selectRecursionAndParentIdAndCategroy(parentId);

        } else {
            return ServerResponse.createByErrorMessage("不是管理员,不能查看子类商品");
        }
    }
}